package com.jason.basicandroid.ui.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;

public class MenuDemo2 extends Activity {
	Menu myMenu = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    	TextView tv = (TextView)findViewById(R.id.tvShow);
    	registerForContextMenu(tv);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.mymenu, menu);
    	
    	myMenu = menu;
    	
    	return true;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.mycontextmenu, menu);
    	
    	// TODO don't know how to set head title in XML?
    	menu.setHeaderTitle("MenuDemo2");
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	int id = item.getItemId();
    	
    	switch(id) {
    	case R.id.menuAppend:
    		appendText("\nhello");
    		break;

    	case R.id.menuItem2:
    		appendText("\nitem 2");
    		
    		break;

    	case R.id.menuClear:
    		emptyText();
    		
    		break;

    	case R.id.menuVisible:
    		appendMenuItemText(item);
    		myMenu.setGroupVisible(R.id.secondaryMenuGroup, true);
    		
    		break;

    	case R.id.menuInvisible:
    		appendMenuItemText(item);
    		myMenu.setGroupVisible(R.id.secondaryMenuGroup, false);
    		
    		break;

    	case R.id.menuEnable:
    		appendMenuItemText(item);
    		myMenu.setGroupEnabled(R.id.secondaryMenuGroup, true);
    		
    		break;

    	case R.id.menuDisable:
    		appendMenuItemText(item);
    		myMenu.setGroupEnabled(R.id.secondaryMenuGroup, false);
    		
    		break;

    	case R.id.menuCheckable:
    		appendMenuItemText(item);
    		myMenu.setGroupCheckable(R.id.secondaryMenuGroup, true, false);
    		
    		break;

    	case R.id.menuUncheckable:
    		appendMenuItemText(item);
    		myMenu.setGroupCheckable(R.id.secondaryMenuGroup, false, false);
    		
    		break;
    		
		default:
			return super.onOptionsItemSelected(item);
    	}
    	
    	return true;
    }
    
    @Override 
    public boolean onContextItemSelected(MenuItem item) {
    	if(R.id.contextMenu1 == item.getItemId()) {
    		appendMenuItemText(item);
    	}
    	return true;
    }
    
    private void appendText(String str) {
    	TextView tv = (TextView)findViewById(R.id.tvShow);
    	tv.setText(tv.getText() + str);
    }
    
    private void appendMenuItemText(MenuItem item) {
    	appendText("\n" + item.getTitle().toString());
    }
    
    private void emptyText() {
    	TextView tv = (TextView)findViewById(R.id.tvShow);
    	tv.setText("");
    }
}